﻿/******************************************************************************
文件名称    :   MySqlOpticsDataProvider.cs
创建日期    :   2011/5/13 6:18:13
功能描述    :   Optics实体数据提供程序MySql实现类
修改日志    :   
    2011-05-13 06:18  马利
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Dare.Utilities.Data;
using Stone.Data.Providers;
using Stone.Components.Entities;

namespace Stone.Data.MySql.Providers
{
    /// <summary>
    /// Optics实体数据提供程序MySql实现类
    /// </summary>
    public class MySqlOpticsDataProvider : OpticsDataProvider
    {
        public MySqlOpticsDataProvider()
            : base()
        {
        }

        public override void Insert(TransactionManager manager, Optics entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_optics(typeBUltrasonicFile_id, CTFile_id, CTvalue, CTUFile_id, KUBIVPFile_id, ECTFile_id, GFRvalue, angle, comment) values(?typeBUltrasonicFile_id, ?CTFile_id, ?CTvalue, ?CTUFile_id, ?KUBIVPFile_id, ?ECTFile_id, ?GFRvalue, ?angle, ?comment);" +
                    "select id, time_stamp from tb_optics where id = last_insert_id();";
                GenericDbHelper.AddParameter(mgr, cmd, "?typeBUltrasonicFile_id", DbType.Int64).Value = entity.TypeBUltrasonicFileId == null ? DBNull.Value : (object)entity.TypeBUltrasonicFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?CTFile_id", DbType.Int64).Value = entity.CTFileId == null ? DBNull.Value : (object)entity.CTFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?CTvalue", DbType.Int32).Value = entity.CTvalue == null ? DBNull.Value : (object)entity.CTvalue.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?CTUFile_id", DbType.Int64).Value = entity.CTUFileId == null ? DBNull.Value : (object)entity.CTUFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?KUBIVPFile_id", DbType.Int64).Value = entity.KUBIVPFileId == null ? DBNull.Value : (object)entity.KUBIVPFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?ECTFile_id", DbType.Int64).Value = entity.ECTFileId == null ? DBNull.Value : (object)entity.ECTFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?GFRvalue", DbType.Decimal).Value = entity.GFRvalue == null ? DBNull.Value : (object)entity.GFRvalue.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?angle", DbType.Decimal).Value = entity.Angle == null ? DBNull.Value : (object)entity.Angle.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;

                //执行数据库操作,并获得主键和更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.Id = reader.GetInt32(0);
                        entity.TimeStamp = reader.GetDateTime(1);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //插入关联
                InsertRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Import(TransactionManager manager, IEnumerable<Optics> entities)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //插入SQL语句
                cmd.CommandText = "insert into tb_optics(id, typeBUltrasonicFile_id, CTFile_id, CTvalue, CTUFile_id, KUBIVPFile_id, ECTFile_id, GFRvalue, angle, comment, time_stamp) values(?id, ?typeBUltrasonicFile_id, ?CTFile_id, ?CTvalue, ?CTUFile_id, ?KUBIVPFile_id, ?ECTFile_id, ?GFRvalue, ?angle, ?comment, ?time_stamp);";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?typeBUltrasonicFile_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?CTFile_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?CTvalue", DbType.Int32);
                GenericDbHelper.AddParameter(mgr, cmd, "?CTUFile_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?KUBIVPFile_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?ECTFile_id", DbType.Int64);
                GenericDbHelper.AddParameter(mgr, cmd, "?GFRvalue", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?angle", DbType.Decimal);
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String);
                GenericDbHelper.AddParameter(mgr, cmd, "?time_stamp", DbType.DateTime);

                //执行数据库操作
                foreach (Optics entity in entities)
                {
                    int i = 0;
                    cmd.Parameters[i++].Value = entity.Id;
                    cmd.Parameters[i++].Value = entity.TypeBUltrasonicFileId == null ? DBNull.Value : (object)entity.TypeBUltrasonicFileId.Value;
                    cmd.Parameters[i++].Value = entity.CTFileId == null ? DBNull.Value : (object)entity.CTFileId.Value;
                    cmd.Parameters[i++].Value = entity.CTvalue == null ? DBNull.Value : (object)entity.CTvalue.Value;
                    cmd.Parameters[i++].Value = entity.CTUFileId == null ? DBNull.Value : (object)entity.CTUFileId.Value;
                    cmd.Parameters[i++].Value = entity.KUBIVPFileId == null ? DBNull.Value : (object)entity.KUBIVPFileId.Value;
                    cmd.Parameters[i++].Value = entity.ECTFileId == null ? DBNull.Value : (object)entity.ECTFileId.Value;
                    cmd.Parameters[i++].Value = entity.GFRvalue == null ? DBNull.Value : (object)entity.GFRvalue.Value;
                    cmd.Parameters[i++].Value = entity.Angle == null ? DBNull.Value : (object)entity.Angle.Value;
                    cmd.Parameters[i++].Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                    cmd.Parameters[i++].Value = entity.TimeStamp;
                    cmd.ExecuteNonQuery();
                }

                //插入关联
                InsertRelations(mgr, cmd, entities);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Update(TransactionManager manager, Optics entity)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //更新SQL语句
                cmd.CommandText = "update tb_optics set typeBUltrasonicFile_id = ?typeBUltrasonicFile_id, CTFile_id = ?CTFile_id, CTvalue = ?CTvalue, CTUFile_id = ?CTUFile_id, KUBIVPFile_id = ?KUBIVPFile_id, ECTFile_id = ?ECTFile_id, GFRvalue = ?GFRvalue, angle = ?angle, comment = ?comment where id = ?id;"
                        + "select time_stamp from tb_optics where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?typeBUltrasonicFile_id", DbType.Int64).Value = entity.TypeBUltrasonicFileId == null ? DBNull.Value : (object)entity.TypeBUltrasonicFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?CTFile_id", DbType.Int64).Value = entity.CTFileId == null ? DBNull.Value : (object)entity.CTFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?CTvalue", DbType.Int32).Value = entity.CTvalue == null ? DBNull.Value : (object)entity.CTvalue.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?CTUFile_id", DbType.Int64).Value = entity.CTUFileId == null ? DBNull.Value : (object)entity.CTUFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?KUBIVPFile_id", DbType.Int64).Value = entity.KUBIVPFileId == null ? DBNull.Value : (object)entity.KUBIVPFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?ECTFile_id", DbType.Int64).Value = entity.ECTFileId == null ? DBNull.Value : (object)entity.ECTFileId.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?GFRvalue", DbType.Decimal).Value = entity.GFRvalue == null ? DBNull.Value : (object)entity.GFRvalue.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?angle", DbType.Decimal).Value = entity.Angle == null ? DBNull.Value : (object)entity.Angle.Value;
                GenericDbHelper.AddParameter(mgr, cmd, "?comment", DbType.String).Value = entity.Comment == null ? DBNull.Value : (object)entity.Comment;
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = entity.Id;

                //执行数据库操作,并获得更新时间戳
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity.TimeStamp = reader.GetDateTime(0);
                        entity.EntityState = Stone.Components.EntityState.Saved;
                    }
                }

                //更新关联
                UpdateRelations(mgr, cmd, entity);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void Delete(TransactionManager manager, long key)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, key);

                //删除SQL语句
                cmd.CommandText = "delete from tb_optics where id = ?id;";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行
                cmd.ExecuteNonQuery();
                /*------------------------------数据库SQL操作结束----------------------------------*/
            });
        }

        public override void DeleteAll(TransactionManager manager, string whereClause)
        {
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------具体SQL操作开始----------------------------------*/

                //先删除关联
                DeleteRelations(mgr, cmd, whereClause);

                //删除SQL语句
                cmd.CommandText = String.Format("delete from tb_optics {0}",
                     ProcessWhereClause(whereClause));

                //执行
                cmd.ExecuteNonQuery();

                /*------------------------------具体SQL操作结束----------------------------------*/
            });
        }

        public override Optics Get(TransactionManager manager, long key)
        {
            Optics entity = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = "select * from tb_optics where id = ?id";
                GenericDbHelper.AddParameter(mgr, cmd, "?id", DbType.Int64).Value = key;

                //执行获取实体
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        entity = Fill(reader, null);
                    }
                }

                //获取关联
                if (entity != null)
                {
                    RetrieveRelations(mgr, cmd, entity);
                    entity.EntityState = Stone.Components.EntityState.Saved;
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return entity;
        }

        public override List<Optics> GetPaged(TransactionManager manager, string whereClause, string orderBy, int pageIndex, int pageSize)
        {
            List<Optics> list = new List<Optics>(pageSize > 0 ? Math.Min(pageSize, 256) : 32);
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select * from tb_optics {0} {1} {2}",
                    ProcessWhereClause(whereClause),
                    ProcessOrderBy(orderBy),
                    ProcessPaging(pageIndex, pageSize));

                //执行获取实体集
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(Fill(reader, null));
                    }
                }

                //获取关联
                RetrieveRelations(mgr, cmd, list);

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return list;
        }

        public override int GetCount(TransactionManager manager, string whereClause)
        {
            int count = 0;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*) from tb_optics {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                count = Convert.ToInt32(cmd.ExecuteScalar());

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return count;
        }

        public override long GetVersion(TransactionManager manager, string whereClause)
        {
            long count = 0;
            DateTime? timeStamp = null;
            Execute(manager, delegate(TransactionManager mgr, DbCommand cmd)
            {
                /*------------------------------数据库SQL操作开始----------------------------------*/
                //获取SQL语句
                cmd.CommandText = String.Format("select count(*), max(time_stamp) from tb_optics {0}",
                    ProcessWhereClause(whereClause));

                //执行获取实体集数量
                using (IDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        count = reader.GetInt64(0);
                        timeStamp = reader.IsDBNull(1) ? (DateTime?)null : reader.GetDateTime(1);
                    }
                }

                /*------------------------------数据库SQL操作结束----------------------------------*/
            });

            return (timeStamp ?? DateTime.MinValue).Ticks ^ count;
        }

        public override Optics Fill(IDataReader reader, Optics entity)
        {
            if (entity == null) entity = new Optics();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                switch (reader.GetName(i))
                {
                    case "id":
                        entity.Id = reader.GetInt64(i);
                        break;
                    case "typeBUltrasonicFile_id":
                        entity.TypeBUltrasonicFileId = reader[i] == DBNull.Value ? entity.TypeBUltrasonicFileId : reader.GetInt64(i);
                        break;
                    case "CTFile_id":
                        entity.CTFileId = reader[i] == DBNull.Value ? entity.CTFileId : reader.GetInt64(i);
                        break;
                    case "CTvalue":
                        entity.CTvalue = reader[i] == DBNull.Value ? entity.CTvalue : reader.GetInt32(i);
                        break;
                    case "CTUFile_id":
                        entity.CTUFileId = reader[i] == DBNull.Value ? entity.CTUFileId : reader.GetInt64(i);
                        break;
                    case "KUBIVPFile_id":
                        entity.KUBIVPFileId = reader[i] == DBNull.Value ? entity.KUBIVPFileId : reader.GetInt64(i);
                        break;
                    case "ECTFile_id":
                        entity.ECTFileId = reader[i] == DBNull.Value ? entity.ECTFileId : reader.GetInt64(i);
                        break;
                    case "GFRvalue":
                        entity.GFRvalue = reader[i] == DBNull.Value ? entity.GFRvalue : reader.GetDecimal(i);
                        break;
                    case "angle":
                        entity.Angle = reader[i] == DBNull.Value ? entity.Angle : reader.GetDecimal(i);
                        break;
                    case "comment":
                        entity.Comment = reader[i] == DBNull.Value ? entity.Comment : reader.GetString(i);
                        break;
                    case "time_stamp":
                        entity.TimeStamp = reader.GetDateTime(i);
                        break;
                }
            }

            entity.EntityState = Stone.Components.EntityState.Saved;
            return entity;
        }

        #region 实体关联操作数据提供类

        #endregion
    }
}
